<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Dataapi extends  Controller
{
    public function _initialize()
    {
        header('Content-Type:text/html; charset=utf-8');
        // if (Session::get('user')==null){
        //     $this->redirect('index/login/login');exit;
        // }
        //判断用户是否登录
        //判断权限
        //登录有效期
    }

    //实时交易订单数据
    // public function real_time_order()
    // {
    //     $partners_and_tools = Db::name('store');
    //     $num = rand(8,15);
    //     $data = $partners_and_tools->order('rand()')->limit($num)->select();
    //     foreach($data as $key=>$value){
    //         $data[$key]['time'] = date('Y-m-d H:i:s',time());
    //         if($value['id']%2==0){
    //             $data[$key]['order_price'] = rand(1,50).'.'.rand(1,9);
    //         }else if($value['id']%5==0){
    //             $data[$key]['order_price'] = (string)rand(50,100);
    //         }else if($value['id']%3==0){
    //             $data[$key]['order_price'] = rand(100,200).'.'.rand(1,9);
    //         }else if($value['id']%7==0){
    //             $data[$key]['order_price'] = (string)rand(200,500);
    //         }else{
    //             $data[$key]['order_price'] = (string)rand(1,500);
    //         }
    //     }
    //     return json_encode($data);
    // }
    public function real_time_order()
    {
        $num = rand(8,15);
        $time = date('Y-m-d',time());
        $unix_time = strtotime($time);

        if ($unix_time+0*60*60<=time() && time()<$unix_time+5*60*60){
            $partners_and_tools = Db::name('ye_store');
            $data = $partners_and_tools->order('rand()')->limit($num)->select();//随机查询几条
            foreach($data as $key=>$value) {
                $data[$key]['time'] = date('Y-m-d H:i:s', time());
                if (strpos($value['store_name'], '烧') !== false || strpos($value['store_name'], '烤') !== false || strpos($value['store_name'], '虾') !== false) {
                    $a = rand(1,5);
                    if ($a > 3) {
                        $data[$key]['order_price'] = rand(30, 300) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(30, 300);
                    }
                } else if (strpos($value['store_name'], '宾') !== false || strpos($value['store_name'], '馆') !== false || strpos($value['store_name'], '别墅') !== false) {
                    $data[$key]['order_price'] = rand(99, 400);
                } else if (strpos($value['store_name'], '网') !== false || strpos($value['store_name'], '鸡排') !== false) {
                    $a = rand(1, 9);
                    if ($a == 1) {
                        $data[$key]['order_price'] = 10;
                    } else if ($a == 2) {
                        $data[$key]['order_price'] = 20;
                    } else if ($a == 3) {
                        $data[$key]['order_price'] = 30;
                    } else if ($a >= 4 && $a <= 7) {
                        $data[$key]['order_price'] = 50;
                    } else if ($a == 8) {
                        $data[$key]['order_price'] = 100;
                    } else if ($a == 9) {
                        $data[$key]['order_price'] = 200;
                    }
                } else if (strpos($value['store_name'], '酒') !== false) {
                    $data[$key]['order_price'] = rand(99, 1200);
                } else {
                    $data[$key]['order_price'] = rand(60, 300);
                }
            }
        }else if($unix_time+5*60*60<=time() && time()<=$unix_time+8*60*60){
            $partners_and_tools = Db::name('zao_store');
            $data = $partners_and_tools->order('rand()')->limit($num)->select();
            foreach($data as $key=>$value) {
                $data[$key]['time'] = date('Y-m-d H:i:s', time());
                if (strpos($value['store_name'], '早') !== false) {
                    $a = rand(1,5);
                    if ($a > 3) {
                        $data[$key]['order_price'] = rand(3, 15) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(3, 15);
                    }
                } else if (strpos($value['store_name'], '面') !== false) {
                    $a = rand(1,5);
                    if ($a > 4) {
                        $data[$key]['order_price'] = rand(7, 35) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(7, 35);
                    }
                } else {
                    $a = rand(1,5);
                    if ($a > 4) {
                        $data[$key]['order_price'] = rand(30, 50);
                    } else {
                        $data[$key]['order_price'] = rand(10, 30);
                    }
                }
            }
        }else if($unix_time+8*60*60<=time() && time()<=$unix_time+21*60*60){
            $partners_and_tools = Db::name('store');
            $data = $partners_and_tools->order('rand()')->limit($num)->select();
            foreach($data as $key=>$value) {
                $data[$key]['time'] = date('Y-m-d H:i:s', time());
                if (strpos($value['store_name'], '早') !== false) {
                    $a = rand(1,5);
                    if ($a > 3) {
                        $data[$key]['order_price'] = rand(3, 15) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(3, 15);
                    }
                } else if (strpos($value['store_name'], '面') !== false) {
                    $a = rand(1,5);
                    if ($a > 4) {
                        $data[$key]['order_price'] = rand(7, 35) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(7, 35);
                    }
                } else if (strpos($value['store_name'], '烧') !== false || strpos($value['store_name'], '烤') !== false || strpos($value['store_name'], '购物') !== false) {
                    $a = rand(1,5);
                    if ($a > 3) {
                        $data[$key]['order_price'] = rand(30, 300) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(30, 300);
                    }
                } else if (strpos($value['store_name'], '宾') !== false || strpos($value['store_name'], '馆') !== false || strpos($value['store_name'], '别墅') !== false || strpos($value['store_name'], '足疗') !== false) {
                    $data[$key]['order_price'] = rand(99, 400);
                } else if (strpos($value['store_name'], '酒店') !== false || strpos($value['store_name'], '国际') !== false || strpos($value['store_name'], '宝贝') !== false || strpos($value['store_name'], '眼镜') !== false) {
                    $data[$key]['order_price'] = rand(500, 1400);
                } else if (strpos($value['store_name'], '果') !== false || strpos($value['store_name'], '便民') !== false || strpos($value['store_name'], '便利') !== false || strpos($value['store_name'], '超市') !== false) {
                    $a = rand(1,5);
                    if ($a > 4) {
                        $data[$key]['order_price'] = rand(150, 300);
                    } else{
                        $data[$key]['order_price'] = rand(20, 150);
                    }
                } else if (strpos($value['store_name'], '网') !== false || strpos($value['store_name'], '鸡排') !== false) {
                    $a = rand(1,9);
                    if ($a == 1) {
                        $data[$key]['order_price'] = 10;
                    } else if ($a == 2) {
                        $data[$key]['order_price'] = 20;
                    } else if ($a == 3) {
                        $data[$key]['order_price'] = 30;
                    } else if ($a >= 4 && $a <= 7) {
                        $data[$key]['order_price'] = 50;
                    } else if ($a == 8) {
                        $data[$key]['order_price'] = 100;
                    } else if ($a == 9) {
                        $data[$key]['order_price'] = 200;
                    }
                } else {
                    $data[$key]['order_price'] = rand(25, 120);
                }
            }
        }else if($unix_time+21*60*60<=time() && time()<=$unix_time+24*60*60){
            $partners_and_tools = Db::name('ye_store');
            $data = $partners_and_tools->order('rand()')->limit($num)->select();//随机查询几条
            foreach($data as $key=>$value) {
                $data[$key]['time'] = date('Y-m-d H:i:s', time());
                if (strpos($value['store_name'], '烧') !== false || strpos($value['store_name'], '烤') !== false || strpos($value['store_name'], '虾') !== false) {
                    $a = rand(1,5);
                    if ($a > 3) {
                        $data[$key]['order_price'] = rand(30, 300) . '.5';
                    } else {
                        $data[$key]['order_price'] = rand(30, 300);
                    }
                } else if (strpos($value['store_name'], '宾') !== false || strpos($value['store_name'], '馆') !== false || strpos($value['store_name'], '别墅') !== false) {
                    $data[$key]['order_price'] = rand(99, 400);
                } else if (strpos($value['store_name'], '网') !== false || strpos($value['store_name'], '鸡排') !== false) {
                    $a = rand(1, 9);
                    if ($a == 1) {
                        $data[$key]['order_price'] = 10;
                    } else if ($a == 2) {
                        $data[$key]['order_price'] = 20;
                    } else if ($a == 3) {
                        $data[$key]['order_price'] = 30;
                    } else if ($a >= 4 && $a <= 7) {
                        $data[$key]['order_price'] = 50;
                    } else if ($a == 8) {
                        $data[$key]['order_price'] = 100;
                    } else if ($a == 9) {
                        $data[$key]['order_price'] = 200;
                    }
                } else if (strpos($value['store_name'], '酒') !== false) {
                    $data[$key]['order_price'] = rand(99, 1200);
                } else {
                    $data[$key]['order_price'] = rand(60, 300);
                }
            }
        }

//        $partners_and_tools = Db::name('store');
//        $data = $partners_and_tools->order('rand()')->limit($num)->select();
//        foreach($data as $key=>$value){
//            $data[$key]['time'] = date('Y-m-d H:i:s',time());
//            if($value['id']%2==0){
//                $data[$key]['order_price'] = rand(1,50).'.5';
//            }else if($value['id']%5==0){
//                $data[$key]['order_price'] = (string)rand(50,100);
//            }else if($value['id']%3==0){
//                $data[$key]['order_price'] = rand(100,200).'.5';
//            }else if($value['id']%7==0){
//                $data[$key]['order_price'] = (string)rand(200,500);
//            }else{
//                $data[$key]['order_price'] = (string)rand(1,500);
//            }
//        }
        return json_encode($data);
    }



    //5点 7200000 ，8点  18000000 ， 10点半 24300000 ，14点 52650000 ，17点 60210000 ，21点 92610000 ，24点 100170000
    //创建今日交易总额
    // 0-5                 5       7200000 7200000
    // 5-8                 3       10800000 18000000
    // 8-10.5 14-17 21-24  8.5     21420000 39420000
    // 10.5-14 17-21       7.5     74250000 113670000
    public function create_trading_sum_today(){
        $trading_sum_today = Db::name('trading_sum_today');
        $time = date('Y-m-d',time());
        $res = $trading_sum_today->where("time='{$time}'")->select();
        if (!empty($res)){
            $unix_time = strtotime($time);
            if ($unix_time+0*60*60<=time() && time()<$unix_time+5*60*60){
                $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(350,450);
            }else if($unix_time+5*60*60<=time() && time()<=$unix_time+8*60*60){
                $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(950,1050);
            }else if(($unix_time+8*60*60<=time() && time()<=$unix_time+10.5*60*60) || ($unix_time+14*60*60<=time() && time()<=$unix_time+17*60*60) || ($unix_time+21*60*60<=time() && time()<=$unix_time+24*60*60)){
                $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(650,750);
            }else if(($unix_time+10.5*60*60<=time() && time()<=$unix_time+14*60*60) || ($unix_time+17*60*60<=time() && time()<=$unix_time+21*60*60)){
                // $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(1950,2050);
                $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(2800,3000);
                // $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(2700,2800);
            }
            // $data['trading_sum_today'] = $res[0]['trading_sum_today']*1+rand(1000,1100);
            $trading_sum_today->where("time='{$time}'")->update($data);
        }else{
            $data['time'] = $time;
            $data['trading_sum_today'] = '0';
            $data['num_sum_today'] = 2000000+rand(500000,700000);
            $trading_sum_today->insert($data);
        }

        $partners_and_tools = Db::name('partners_and_tools');
        $data1 = $partners_and_tools->where("id=1")->field('store_num,merchant_num,cashier_num,terminal_num,time,man_num,men_num')->find();
        $today = date("Y-m-d",time());
        if ($data1['time']<$today){
            $data1['store_num'] = (string)(round($data1['store_num']*1)+rand(500,1000));
            $data1['merchant_num'] = (string)round($data1['store_num']/2.6);
            $data1['cashier_num'] = (string)round($data1['store_num']*2.8);
            $data1['terminal_num'] = (string)round($data1['cashier_num']*1.2);
            $data1['man_num'] = (string)round($data1['cashier_num']/3.2);//男收银员数量
            $data1['men_num'] = (string)($data1['cashier_num']*1-$data1['man_num']*1);//女收银员数量
            $data1['time'] = (string)($today);
            $partners_and_tools->where("id=1")->update($data1);

            $show_add_data = Db::name('show_add_data');
            $show_add_data->insert($data1);

        }
        echo 1111;
    }

    //返回今日交易总额
    public function trading_sum_today(){
        $trading_sum_today = Db::name('trading_sum_today');
        $time = date('Y-m-d',time());
        $res = $trading_sum_today->where("time='{$time}'")->select();
        return $res[0]['trading_sum_today'];
    }

    //当时交易数据动态图用
    public function trading_this_time(){
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);//带毫秒的时间戳
        $data['money'] = rand(500,3500);
        $data['time'] = $msectime;
        return json_encode($data);
    }

    //近七天交易数据展示
    public function nearly_seven_day(){
        $trading_sum_today = Db::name('trading_sum_today');
        $res = $trading_sum_today->limit(8)->order("id desc")->field('time,num_sum_today,trading_sum_today')->select();
        array_shift($res);//去掉数组第一个元素
        sort($res);//按索引从小到大排序
        return json_encode($res);
    }

}
